package speelotheek.models;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;

import speelotheek.components.Database;

/*
 * @author Jeroen
 * This class gets members / toys from DB and creates a data object to fill up tables etc
 */
public class Overzicht {

	private Vector columnNames = new Vector();
	private Vector data = new Vector();
	private String type;
	private String q;
	private String search;
	private String conditions = "";
	private String filter;
	
	//constructor
	public Overzicht(){
	}

	public void setType(String type){
		this.type = type;
	}
	
	public String getType(){
		return this.type;
	}
	
	public void setSearch(String search){
		this.search = search;
	}
	
	public String getSearch(){
		return this.search;
	}
	
	public String getSelectedButtonText(ButtonGroup buttonGroup) {
        for (Enumeration<AbstractButton> buttons = buttonGroup.getElements(); buttons.hasMoreElements();) {
            AbstractButton button = buttons.nextElement();

            if (button.isSelected()) {
                return button.getText();
            }
        }

        return null;
    }
	
	public void setFilter(String filter){
		this.filter = filter;
	}
	
	public void createVectors(){
				
		if(this.type.equals("member")){
			if(this.search != null){
				conditions = "WHERE nummer LIKE '%" + this.search  + "%' OR naam LIKE '%" + this.search  + "%' OR adres LIKE '%" + this.search  + "%' OR postcode LIKE '%" + this.search  + "%' OR woonplaats LIKE '%" + this.search  + "%' OR email LIKE '%" + this.search  + "%' OR telefoon LIKE '%" + this.search  + "%' OR startlidmaatschap LIKE '%" + this.search  + "%' or eindlidmaatschap LIKE '%" + this.search  + "%'";
			}
			
			q = "SELECT * FROM lid " + conditions;
			
		} else if(this.type.equals("toy")){
			
			if(this.search != null){
				conditions = "WHERE s.code LIKE '%" + this.search  + "%' OR c.naam LIKE '%" + this.search  + "%' OR s.omschrijving LIKE '%" + this.search  + "%' OR s.naam LIKE '%" + this.search  + "%' OR s.aankoopdatum LIKE '%" + this.search  + "%' OR s.aankoopbedrag LIKE '%" + this.search  + "%' OR s.aantalonderdelen LIKE '%" + this.search  + "%'";
			}
			
			q = "SELECT s.code AS speelgoedcode, c.naam AS categorie, s.naam AS naam, s.omschrijving, s.aankoopdatum, s.aankoopbedrag, s.aantalonderdelen, u.bijzonderheden FROM speelgoed AS s LEFT JOIN leeftijdscategorie AS c ON ( s.categorie = c.id ) LEFT JOIN uitlening AS u ON ( s.code = u.speelgoed ) " + conditions + " GROUP BY s.code";

		} else if(this.type.equals("lending")){
			if(this.search != null){
				conditions = "WHERE u.id LIKE '%" + this.search  + "%' OR l.naam LIKE '%" + this.search  + "%' OR l.nummer LIKE '%" + this.search  + "%' OR s.naam LIKE '%" + this.search  + "%' OR s.code LIKE '%" + this.search  + "%' OR u.uitleendatum LIKE '%" + this.search  + "%' OR u.innamedatum LIKE '%" + this.search  + "%' OR u.terugbrengdatum LIKE '%" + this.search  + "%' or u.bijzonderheden LIKE '%" + this.search  + "%'";
			}
			
			q = "SELECT u.id AS uitleningcode, l.naam AS lid, l.nummer AS lidnummer, s.naam AS speelgoed, s.code AS speelgoedcode, u.uitleendatum, u.innamedatum, u.terugbrengdatum, u.bijzonderheden FROM uitlening AS u LEFT JOIN lid AS l ON ( u.lid = l.nummer ) LEFT JOIN speelgoed AS s ON ( u.speelgoed = s.code )  " + conditions + " ORDER BY u.uitleendatum DESC";
		}

		ResultSet resultSet = Database.executeSelectQuery(q);
		try{
						
			ResultSetMetaData metaData = resultSet.getMetaData();
	        int columns = metaData.getColumnCount();
	        	        
	        for (int i = 1; i <= columns; i++) {
	            columnNames.addElement(metaData.getColumnLabel(i));
	        }
	        if(this.type.equals("toy")){
	        	columnNames.addElement("uitgeleend");
	        }
	        
	        while (resultSet.next()) {
	            Vector row = new Vector(columns);
	            for (int i = 1; i <= columns; i++) {
	                row.addElement(resultSet.getObject(i));
	            }
	            
	            if(this.type.equals("toy")){
		            //Check of Speelgoed is uitgeleend
		            String speelgoedcode = (String) row.get(0);
		            Speelgoed speelgoed = new Speelgoed();
			        speelgoed.setCode(speelgoedcode);
			        
			        if(speelgoed.isUitgeleend()){
			        	row.addElement("ja");
			        } else {
			        	row.addElement("nee");
			        }
	            }
	            
	            //Check if we need to filter some data
	            if(this.filter != null){
	            	if(this.filter.equals("Geen Filter")){
	            		data.addElement(row);
	            	} else if(this.filter.equals("Uitgeleend")){
	            		if(this.type.equals("lending")){
	            			if(row.get(6) == null){
	            				data.addElement(row);
	            			}
	            		} else {
	            			if(row.get(8).equals("ja")){
		            			data.addElement(row);
		            		}
	            		}
	            	} else {
	            		if(this.type.equals("lending")){
	            			if(row.get(6) != null){
	            				data.addElement(row);
	            			}	            			
	            		} else {
		            		if(row.get(8).equals("nee")){
		            			data.addElement(row);
		            		}
	            		}
	            	}
	            } else {
	            	data.addElement(row);
	            }
	            
	        }
	        resultSet.close();
	    } catch(Exception e){
	    	System.out.println("Oh boy foutje in Overzicht.java");
	    	System.out.println(e);
	    }
	}
	
	public Vector getColumns(){
		return this.columnNames;
	}
	
	public Vector getData(){
		return this.data;
	}
}
